package 面向对象;


import java.util.Scanner;

public class OrderMGR {
    public static void main(String[] args) {
        //初始化4条订单数据

        Scanner sc = new Scanner(System.in);
        String[] names = new String[4]; // 保存订餐人名称
        String[] dishes = new String[]{"重庆辣子鸡", "小鸡炖蘑菇", "红烧狮子头", "精品红烧肉"}; // 保存菜品名及份数
        int[] times = new int[]{10, 11, 12, 18}; // 保存送餐时间
        String[] addresses = new String[]{"上海路", "天成路", "太仓路", "中思路"}; // 保存送餐地址
        int[] statuses = new int[]{0, 0, 1, 1}; // 保存订单状态： 0：已预订 1：已完成
        double[] totalPrices = new double[]{45.0d, 55.0, 48.0d, 65.0d}; // 保存订单的总金额
        double[]orderDates={4,5,6,7};
        double[] prices= {55,54,63,68};
        System.out.println("\n欢迎使用“吃货联盟订餐系统”");
        // 循环：显示菜单，根据用户选择的数字执行相应功能
        int num = 1;// 用户输入0返回主菜单否则退出系统
        boolean isExit = false;// 标记用户
            do {
                System.out.println("************************");
                System.out.println("1、我要订餐");
                System.out.println("2、查看餐袋");
                System.out.println("3、签收订单");
                System.out.println("4、删除订单");
                System.out.println("5、我要点赞");
                System.out.println("6、退出系统");
                System.out.println("************************");
                System.out.print("请选择：");
               num = sc.nextInt();// 用户输入所要选择进行的操作

                /* 使用switch结构来建立选择内容 */

                switch (num) {
                    case 1:// 我要订餐
                        System.out.println("***我要订餐***");
                        boolean isAdd = false;// 记录是否可以点餐
                        for (int i = 0; i < names.length; i++) {
                            if (names[i] == null) {
                                isAdd = false;
                                System.out.print("请输入订餐人姓名：");
                                String name = sc.next();
                                System.out.println("序号" + "\t" + "菜名" + "\t\t" + "单价" + "\t" + "点赞数");
                                for (int j = 0; j < dishes.length; j++) {
                                    System.out.println(
                                            (j + 1) + "\t" + dishes[j] + "\t\t" + prices[j] + "元" + "\t" + orderDates[j]);
                                }
                                System.out.print("请选择您要点的菜品编号：");
                                int chooseDish = sc.nextInt();
                                String dishMeg = dishes[chooseDish - 1];
                                System.out.print("请选择您需要的份数：");
                                int number = sc.nextInt();
                                System.out.print("请输入送餐时间（送餐时间是10点至20点间整点送餐）：");
                                int time = sc.nextInt();
                                while (time < 10 || time > 20) {
                                    System.out.print("您的输入有误，请输入10-20间的整数");
                                    time = sc.nextInt();
                                }
                                System.out.print("请输入送餐地址：");
                                String address = sc.next();
                                // 默认可以预定，不用写订单状态
                                System.out.println("订餐成功！");
                                System.out.println("您定的是：" + dishes[chooseDish - 1] + number + "份");
                                System.out.println("送餐时间：" + time + "点");
                                double sumPrice = prices[chooseDish - 1] * number;// 计算餐费
                                double sendMoney = (sumPrice >= 50) ? 0 : 5;// 计算送餐费
                                System.out.println("餐费：" + sumPrice + "元，" + "送餐费" + sendMoney + "元，" + "总计："
                                        + (sumPrice + sendMoney) + "元。");
                                // 添加数据
                                names[i] = name;
                                dishes[i] = dishMeg;
                                times[i] = time;
                                orderDates[i] = number;
                                addresses[i] = address;
                                totalPrices[i] = sumPrice + sendMoney;
                                break;
                            }
                            if (isAdd) {
                                System.out.println("对不起，您的餐袋已满！");
                            }
                        }
                        break;
                    case 2:// 查看餐袋
                        System.out.println("***查看餐袋***");
                        System.out.println("序号\t订餐人\t餐品名称\t餐品数量\t送餐时间\t送餐地址\t总金额\t订单状态");
                        for (int i = 0; i < names.length; i++) {
                            if (names[i] != null) {
                                String state = (statuses[i] == 0) ? "已预订" : "已完成";
                                System.out.println((i + 1) + "\t" + names[i] + "\t" + dishes[i] + "\t" + dishes[i] + "份"
                                        + "\t" + times[i] + "\t" + addresses[i] + "\t" + totalPrices[i] + "元" + "\t" + state);
                            }
                        }
                        break;
                    case 3:// 签收订单
                        System.out.println("***签收订单***");
                        boolean isSignFind = false;
                        System.out.print("请选择要签收的订单序号：");
                        int sign = sc.nextInt();
                        for (int i = 0; i < names.length; i++) {
                            if (names[i] != null && statuses[i] == 0 && sign == i + 1) {
                                statuses[i] = 1;
                                System.out.println("订单签收成功！");
                                isSignFind = true;// 标记已找到的订单
                            } else if (names[i] != null && statuses[i] == 1 && sign == i + 1) {
                                System.out.println("您选择的订单已完成签收，不能再次签收！");
                                isSignFind = true;// 标记已找到的订单
                            }
                        }
                        if (!isSignFind) {
                            System.out.println("您选择的订单不存在！");
                        }
                        break;
                    case 4:// 删除订单
                        System.out.println("***删除订单***");
                        boolean isDelFind = false;
                        System.out.print("请输入要删除的订单序号：");
                        int delID = sc.nextInt();
                        for (int i = 0; i < names.length; i++) {
                            // 根据状态值判断能不能删除，只有已完成的才能删除
                            if (names[i] != null && statuses[i] == 1 && delID == i + 1) {
                                isDelFind = true;
                                for (int j = delID - 1; j < names.length - 1; j++) {
                                    names[j] = names[j + 1];
                                    dishes[j] = dishes[j + 1];
                                    totalPrices[j] = totalPrices[j + 1];
                                    times[j] = times[j + 1];
                                    addresses[j] = addresses[j + 1];
                                    statuses[j] = statuses[j + 1];
                                    totalPrices[j] = totalPrices[j + 1];
                                }
                                // 将最后一位清空
                                names[names.length - 1] = null;
                                dishes[names.length - 1] = null;
                                totalPrices[names.length - 1] = 0;
                                times[names.length - 1] = 0;
                                addresses[names.length - 1] = null;
                                statuses[names.length - 1] = 0;
                               totalPrices[names.length - 1] = 0;
                                System.out.println("删除订单成功！");
                                break;
                            } else if (names[i] != null && statuses[i] == 0 && delID == i + 1) {
                                isDelFind = true;
                                System.out.println("您选择的订单未签收，不能删除！");
                                break;
                            }
                        } // 未找到的订单不能删除
                        if (!isDelFind) {
                            System.out.println("您要删除的订单不存在！");
                        }
                        break;
                    case 5:// 我要点赞
                        System.out.println("***我要点赞***");
                        // 显示菜品信息
                        System.out.println("序号\t菜名\t单价");
                        for (int i = 0; i < dishes.length; i++) {
                            String priaiseNum = (totalPrices[i] > 0) ? totalPrices[i] + "赞" : "";
                            System.out.println((i + 1) + "\t" + dishes[i] + "\t" + prices[i] + "元" + priaiseNum);
                        }
                        System.out.print("请选择您要点赞的菜品序号：");
                        int number = sc.nextInt();
                       totalPrices[number - 1]++;
                        System.out.println("点赞成功");
                        break;
                    case 6:// 退出系统
                        isExit = true;
                        break;
                    default:// 退出系统
                        isExit = true;
                        break;
                }

                // 返回主界面
                if (!isExit) {
                    System.out.print("输入0返回：");
                    num = sc.nextInt();
                } else {
                    break;
                }
            } while (num == 0);
            System.out.println("谢谢惠顾，欢迎您再次使用！");
            sc.close();
        }

    }
